package com.jeeplus.test.jlnu.student.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeeplus.test.jlnu.student.domain.entity.BsssStudentResume;
import com.jeeplus.test.jlnu.student.mapper.BsssStudentResumeMapper;
import com.jeeplus.test.jlnu.student.service.BsssStudentResumeService;
import com.jeeplus.test.tools.resp.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BsssStudentResumeServiceImpl extends ServiceImpl<BsssStudentResumeMapper, BsssStudentResume> implements BsssStudentResumeService {
    @Autowired
    private BsssStudentResumeMapper bsssStudentResumeMapper;

    @Override
    public BsssStudentResume getBsssStudentResume(String studentId, String flag) {
        LambdaQueryChainWrapper<BsssStudentResume> wrapper = new LambdaQueryChainWrapper<>(bsssStudentResumeMapper);
        wrapper.eq(BsssStudentResume::getStudentId,studentId);
        wrapper.eq(BsssStudentResume::getFlag,flag);
        return wrapper.one();
    }

    @Override
    public Boolean bsssStudentResumeUpdate(BsssStudentResume bsssStudentResume) {
        LambdaUpdateChainWrapper<BsssStudentResume> wrapper = new LambdaUpdateChainWrapper<>(bsssStudentResumeMapper);
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume1StartDate())){
            wrapper.set(BsssStudentResume::getPersonalResume1StartDate,bsssStudentResume.getPersonalResume1StartDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume1EndDate())){
            wrapper.set(BsssStudentResume::getPersonalResume1EndDate,bsssStudentResume.getPersonalResume1EndDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume1Institution())){
            wrapper.set(BsssStudentResume::getPersonalResume1Institution,bsssStudentResume.getPersonalResume1Institution());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume1Position())){
            wrapper.set(BsssStudentResume::getPersonalResume1Position,bsssStudentResume.getPersonalResume1Position());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume2StartDate())){
            wrapper.set(BsssStudentResume::getPersonalResume2StartDate,bsssStudentResume.getPersonalResume2StartDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume2EndDate())){
            wrapper.set(BsssStudentResume::getPersonalResume2EndDate,bsssStudentResume.getPersonalResume2EndDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume2Institution())){
            wrapper.set(BsssStudentResume::getPersonalResume2Institution,bsssStudentResume.getPersonalResume2Institution());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume2Position())){
            wrapper.set(BsssStudentResume::getPersonalResume2Position,bsssStudentResume.getPersonalResume2Position());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume3StartDate())){
            wrapper.set(BsssStudentResume::getPersonalResume3StartDate,bsssStudentResume.getPersonalResume3StartDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume3EndDate())){
            wrapper.set(BsssStudentResume::getPersonalResume3EndDate,bsssStudentResume.getPersonalResume3EndDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume3Institution())){
            wrapper.set(BsssStudentResume::getPersonalResume3Institution,bsssStudentResume.getPersonalResume3Institution());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume3Position())){
            wrapper.set(BsssStudentResume::getPersonalResume3Position,bsssStudentResume.getPersonalResume3Position());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume4StartDate())){
            wrapper.set(BsssStudentResume::getPersonalResume4StartDate,bsssStudentResume.getPersonalResume4StartDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume4EndDate())){
            wrapper.set(BsssStudentResume::getPersonalResume4EndDate,bsssStudentResume.getPersonalResume4EndDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume4Institution())){
            wrapper.set(BsssStudentResume::getPersonalResume4Institution,bsssStudentResume.getPersonalResume4Institution());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume4Position())){
            wrapper.set(BsssStudentResume::getPersonalResume4Position,bsssStudentResume.getPersonalResume4Position());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume5StartDate())){
            wrapper.set(BsssStudentResume::getPersonalResume5StartDate,bsssStudentResume.getPersonalResume5StartDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume5EndDate())){
            wrapper.set(BsssStudentResume::getPersonalResume5EndDate,bsssStudentResume.getPersonalResume5EndDate());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume5Institution())){
            wrapper.set(BsssStudentResume::getPersonalResume5Institution,bsssStudentResume.getPersonalResume5Institution());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPersonalResume5Position())){
            wrapper.set(BsssStudentResume::getPersonalResume5Position,bsssStudentResume.getPersonalResume5Position());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentResume.getYouthLeagueJoinInfo())){
            wrapper.set(BsssStudentResume::getYouthLeagueJoinInfo,bsssStudentResume.getYouthLeagueJoinInfo());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentResume.getPartyJoinInfo())){
            wrapper.set(BsssStudentResume::getPartyJoinInfo,bsssStudentResume.getPartyJoinInfo());
        }
        //todo 后期新增学籍变更信息 字段 ↓

        //根据id修改
        wrapper.eq(BsssStudentResume::getId, bsssStudentResume.getId());
        return wrapper.update();
    }

    @Override
    public R addBsssStudentResume(Map<String, Object> requestData) {
        try {

            List<BsssStudentResume> dataValue = (List<BsssStudentResume>) requestData.get("data");
            if (dataValue.size() <= 0) {
                return R.error("400", "简历数据不能为空");
            }
            if (dataValue.size() > 3) {
                return R.error("400", "只允许添加3条简历");
            }

            String studentIdObj = (String) requestData.get("studentId");
            if (studentIdObj == null || studentIdObj == "") {
                return R.error("400", "学号不能为null");
            }


            Object rawData = requestData.get("data");
            ObjectMapper objectMapper = new ObjectMapper();
            List<BsssStudentResume> list = objectMapper.convertValue(
                    rawData,
                    objectMapper.getTypeFactory().constructCollectionType(List.class, BsssStudentResume.class)
            );

            populateResumeFields(list, requestData);

            int affectedRows = bsssStudentResumeMapper.addBsssStudentResume(list);
            if (affectedRows <= 0) {
                return R.error("500", "简历提交失败");
            }

            return R.suc();
        } catch (IllegalArgumentException e) {
            // 类型转换错误（如 JSON 字段与类不匹配）
            return R.error("400", "数据格式错误: " + e.getMessage());
        } catch (DataAccessException e) {
            // 数据库异常（如唯一约束冲突）
            return R.error("500", "数据库错误: " + e.getCause().getMessage());
        } catch (Exception e) {
            // 其他未知异常
            return R.error("500", "系统内部错误");
        }
    }

    @Override
    public int add(BsssStudentResume bsssStudentResume) {
        return bsssStudentResumeMapper.add(bsssStudentResume);
    }

    @Override
    public List<BsssStudentResume> bsssStudentResumeList(String studentId) {
        return bsssStudentResumeMapper.getResumeByStudentId(studentId);
    }

    @Override
    public int deleteResumeById(int id) {
        return bsssStudentResumeMapper.deleteResumeById(id);
    }

    @Override
    public int updateResumeById(BsssStudentResume bsssStudentResume) {
        return bsssStudentResumeMapper.updateResumeById(bsssStudentResume);
    }

    @Override
    public int batchUpdateByStudentIds(BsssStudentResume bsssStudentResume) {
        return bsssStudentResumeMapper.batchUpdateByStudentIds(bsssStudentResume);
    }

    @Override
    public Page<BsssStudentResume> getResumeInfoPage(BsssStudentResume bsssStudentResume, Page<BsssStudentResume> page) {
        return bsssStudentResumeMapper.getResumeInfoPage(bsssStudentResume,page);
    }

    @Override
    public BsssStudentResume getStudentResumeByStudentId(BsssStudentResume bsssStudentResume) {
        return bsssStudentResumeMapper.getStudentResumeByStudentId(bsssStudentResume);
    }

    // 辅助方法：填充字段
    private void populateResumeFields(List<BsssStudentResume> list, Map<String, Object> requestData) {
        String autobiography = (String) requestData.getOrDefault("autobiography", "");
        String youthLeagueJoinInfo = (String) requestData.getOrDefault("youthLeagueJoinInfo", "");
        String partyJoinInfo = (String) requestData.getOrDefault("partyJoinInfo", "");
        String studentId = (String) requestData.getOrDefault("studentId", "");

        list.forEach(resume -> {
            resume.setAutobiography(autobiography);
            resume.setYouthLeagueJoinInfo(youthLeagueJoinInfo);
            resume.setPartyJoinInfo(partyJoinInfo);
            resume.setStudentId(studentId);
        });
    }



}
